package vn.intelsoft.vlogvn.db;

import java.util.ArrayList;
import java.util.List;

import vn.intelsoft.vlogvn.instance.Log;
import vn.intelsoft.vlogvn.instance.Vlog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter {
	private Context mContext;

	private final static int DB_VERSION = 1;
	private String DB_NAME = "vlogvn";

	private DbHelper mHelper;

	private SQLiteDatabase mDB;

	private final static String TABLE_NAME = "vlog";
	private final static String ID = "id";
	private final static String TITLE = "title";
	private final static String THUMB = "thumbnailHd";
	private final static String UPLOAD_TIME = "uploadTime";
	private final static String VIEW_COUNT = "viewCount";
	private final static String LIKE_COUNT = "likeCount";
	private final static String DURATION = "duration";

	public DbAdapter(Context context) {
		this.mContext = context;
	}

	private class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + ID
					+ " INTEGER, " + TITLE + " TEXT, " + THUMB + " TEXT, "
					+ UPLOAD_TIME + " TEXT, " + VIEW_COUNT + " INTEGER, "
					+ LIKE_COUNT + " INTEGER, " + DURATION + " INTEGER " + ");";

			Log.i(TABLE_CREATE);
			try {
				db.execSQL(TABLE_CREATE);
			} catch (Exception e) {
				Log.e(e.getMessage());
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			String TABLE_DROP = "DROP TABLE IF EXISTS" + TABLE_NAME;
			db.execSQL(TABLE_DROP);

			onCreate(db);
		}

	}

	public DbAdapter open() {
		mHelper = new DbHelper(mContext, DB_NAME, null, DB_VERSION);
		this.mDB = mHelper.getWritableDatabase();
		return this;
	}

	public void empty() {
		String empty = "DROP TABLE" + TABLE_NAME;
		mDB.execSQL(empty);
	}

	public void close() {
		mDB.close();
	}

	public long insert(Vlog v) {
		open();

		ContentValues values = new ContentValues();

		values.put(ID, v.vlogId);
		values.put(TITLE, v.title);
		values.put(THUMB, v.thumbnailHd);
		values.put(UPLOAD_TIME, v.uploadTime);
		values.put(VIEW_COUNT, v.viewCount);
		values.put(LIKE_COUNT, v.likeCount);
		values.put(DURATION, v.duration);

		long rowId = 0;
		try {
			rowId = mDB.insert(TABLE_NAME, null, values);
		} catch (Exception e) {
			Log.e(e.getMessage());
		}
		close();
		return rowId;
	}

	public List<Vlog> getAllVlog() {
		List<Vlog> vlogs = new ArrayList<Vlog>();
		
		open();
		try {
			Cursor cursor = mDB.query(TABLE_NAME, new String[] { ID, TITLE,
					THUMB, UPLOAD_TIME, VIEW_COUNT, LIKE_COUNT, DURATION },
					null, null, null, null, null);

			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				do {
					Vlog vlog = new Vlog(cursor.getString(cursor
							.getColumnIndex(ID)), cursor.getString(cursor
							.getColumnIndex(TITLE)), null, null,
							cursor.getString(cursor.getColumnIndex(THUMB)),
							null, null, cursor.getString(cursor
									.getColumnIndex(UPLOAD_TIME)),
							cursor.getInt(cursor.getColumnIndex(VIEW_COUNT)),
							cursor.getInt(cursor.getColumnIndex(LIKE_COUNT)),
							cursor.getInt(cursor.getColumnIndex(DURATION)));
					vlogs.add(vlog);
				} while (cursor.moveToNext());
				cursor.close();
			}

		} catch (Exception e) {
			Log.e(e.getMessage());
		}
		close();
		
		return vlogs;
	}
	
	public int deleteVlogById(String id) {
		open();
		int row = 0;
		try {
			row = mDB.delete(TABLE_NAME, ID + " like '" + id + "'", null);
		} catch (Exception e) {
			Log.e(e.getMessage());
		}
		close();
		return row;
	}

}
